#include "stdafx.h"
#include "CBackLightStatu.h"
#include "CGraphicMgr.h"
#include "ioctl_code.h"
#include "CSlider.h"
#include <winioctl.h>

CBackLightStatu::CBackLightStatu(WCHAR * name):IStatu(name)
{
}

CBackLightStatu::~CBackLightStatu(void)
{
}

 bool CBackLightStatu::EnterStatu()
 {

	  CSlider *pSlider=(CSlider*)GRAPHICMGR.GetElement(L"backlight_slider");
	  if (pSlider)
	  pSlider->SetRange(1,10);
	 
	 
	 //m_pBLK = CreateFile(L"BKL1:", GENERIC_READ | GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);



	 //if(m_pBLK == INVALID_HANDLE_VALUE)
	 //{
		//
		// return FALSE;
	 //}
  ////  int iGetValue;

	 //if(DeviceIoControl(m_pBLK, IOCTL_BLK_GETPOWER, &m_SetValue, sizeof(int), &m_GetValue,  sizeof(int), &m_ulRetSize,NULL))
	 //{
		//
		// m_SetValue = m_GetValue;
	 //}
	
	 m_SetValue=GlobalStatic::GetBacklight();
	pSlider->SetPos(m_SetValue);

	 return TRUE;
 }

 bool CBackLightStatu::EvaluateClickEvent(const WCHAR* elementName)
 {
	 if (wcscmp(elementName,L"backlight_btn_dec") == 0)
		 return BackLightDown();
	 if (wcscmp(elementName,L"backlight_btn_inc") == 0)
		 return BackLightUp();
      return false;
 }

 bool CBackLightStatu::BackLightUp()
 {
	 m_SetValue=m_SetValue+1;
	 if (m_SetValue>9)
	 {
		 m_SetValue=9;
	 }
	 GlobalStatic::SetBacklight(m_SetValue);

	/* DeviceIoControl(m_pBLK, IOCTL_BLK_SETPOWER, &m_SetValue, sizeof(int), &m_GetValue,  sizeof(int), &m_ulRetSize,NULL);*/

	 CSlider *pSlider=(CSlider*)GRAPHICMGR.GetElement(L"backlight_slider");
     if (pSlider)
	 pSlider->SetPos(m_SetValue);
	 GRAPHICMGR.SetUpdateElements(L"backlight_slider",L"none");
		 return true;

 }
 bool CBackLightStatu::ExitStatu()
 {

	 
	 return true;
 }
bool CBackLightStatu::BackLightDown()
{

	CSlider *pSlider=(CSlider*)GRAPHICMGR.GetElement(L"backlight_slider");
   
	m_SetValue=m_SetValue-1;;
		if (m_SetValue<=0)
		{
			m_SetValue=0;
		}
		  
		   GlobalStatic::SetBacklight(m_SetValue);
	//DeviceIoControl(m_pBLK, IOCTL_BLK_SETPOWER, &m_SetValue, sizeof(int), &m_GetValue,  sizeof(int), &m_ulRetSize,NULL);
        if (pSlider)
		pSlider->SetPos(m_SetValue);
		GRAPHICMGR.SetUpdateElements(L"backlight_slider",L"none");
			return true;
}
bool CBackLightStatu::EvaluateSliderMoveEvent(const WCHAR* elementName)
{



	if (wcscmp(elementName,L"backlight_slider") == 0)
		return BackLightSliderClick();
	
	return false;
}

bool CBackLightStatu::BackLightSliderClick()
{
	CSlider *pSlider=(CSlider*)GRAPHICMGR.GetElement(L"backlight_slider");
	int npos=pSlider->GetPos();

    m_SetValue=npos;

	//DeviceIoControl(m_pBLK, IOCTL_BLK_SETPOWER, &m_SetValue, sizeof(int), &m_GetValue,  sizeof(int), &m_ulRetSize,NULL);

    GlobalStatic::SetBacklight(m_SetValue);
	if (pSlider)
	pSlider->SetPos(m_SetValue);
	GRAPHICMGR.SetUpdateElements(L"backlight_slider",L"none");
	return true;
}